﻿using System.Linq;
using Autofac;
using Autofac.Core;
using Dexter.Logger.Contracts;

namespace Dexter.Dependency.Autofac.Module {
	public class LoggingModule : global::Autofac.Module {
		protected override void AttachToComponentRegistration ( IComponentRegistry registry , IComponentRegistration registration ) {
			registration.Preparing += OnComponentPreparing;
		}

		static void OnComponentPreparing ( object sender , PreparingEventArgs e ) {
			var t = e.Component.Activator.LimitType;
			e.Parameters = e.Parameters.Union ( new[] {
				new ResolvedParameter ( ( p , i ) => p.ParameterType == typeof ( ILogger ) , ( p , i ) => i.Resolve<ILoggerFactory>().Create ( t ) )
			} );
		}
	}
}